package com.pktechan.api.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;
import java.util.UUID;

/**
 * Created by gj.hu on 2015/11/10.
 */
public class RandomUtil {

    private static Logger logger = LoggerFactory.getLogger(RandomUtil.class);

    public static String createR(int length) {
        String s = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            s += (int) Math.abs(random.nextInt()) % 10 + "";
        }
        return s;
    }

    /**
     * 根据用户OpenId生成唯一字符串，作为登录态的主键
     */
    public static String createSessionId(String openId) {
        return UUID.nameUUIDFromBytes(openId.getBytes()).toString().replace("-", "");
    }

    public static void main(String[] args) {
        String sessionId = RandomUtil.createSessionId("oG6-S0_xyHwTknTZ8Js0y6v0KrMQ");
        logger.debug("sessionId=" + sessionId);
    }
}
